/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.wx.statweb.action;

import cn.ac.iie.di.wx.statweb.common.RuntimeEnv;
import cn.ac.iie.di.wx.statweb.configuration.Configuration;
import cn.ac.iie.di.wx.statweb.utils.DateUtil;
import cn.ac.iie.di.wx.statweb.utils.PicCopy;
import cn.ac.iie.di.wx.statweb.utils.RedisUtil;
import com.alibaba.fastjson.JSON;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.HashMap;
import java.util.logging.Level;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

/**
 *
 * @author Administrator
 */
public class BaiduTieBaWebStatAction {

    static Logger logger = Logger.getLogger(BaiduTieBaWebStatAction.class.getName());
    private String zxid;
    private String forum_type;
    private String trunk;

    public String baidu() {
        HttpServletRequest request = ServletActionContext.getRequest();
        forum_type = request.getParameter("forum_type");
        trunk = request.getParameter("trunk");
        logger.info(forum_type);
        return "success";
    }

    public String wx_network() {
        HttpServletRequest request = ServletActionContext.getRequest();
        zxid = request.getParameter("id");
        return "success";
    }

    public String getForum_type() {
        return forum_type;
    }

    public void setForum_type(String forum_type) {
        this.forum_type = forum_type;
    }

    public String baidu_all_data() {

        PrintWriter out = null;
        String trunkval = null;
        try {
            HttpServletRequest request = ServletActionContext.getRequest();
            String selectedDate = request.getParameter("selectDate");
            String head = request.getParameter("head");//区分数据类型
            String tail = request.getParameter("tail");//区分数据时间
            String webpath = request.getParameter("webpath");
            //针对专线接入的
            String zxidvalue = request.getParameter("zxid");
            trunkval = request.getParameter("baidu_all_trunk");
            String home_path = PicCopy.readProperties(head);//获取根目录
            if (trunkval != null) {
                if (trunkval.equals("yizhuang")) {//默认为数北
                    home_path = home_path;//区分数北亦庄，mmp
                }
            }

            boolean iszxjrFlag = false;
            if (zxidvalue != null && !zxidvalue.equals("")) {
                //确定是专线监控内容，需要新加一层
                logger.info("专线监控子目录" + zxidvalue);
                iszxjrFlag = true;
            }
            //根据日期选择图片并
            HttpServletResponse response = ServletActionContext.getResponse();
            response.reset();
            response.setContentType("text/html;charset=UTF-8");
            response.setCharacterEncoding("UTF-8");//防止弹出的信息出现乱码getjobinfo
            out = response.getWriter();
            String web_path = "";
            if (selectedDate.equals("0")) {//实时
                if (iszxjrFlag) {//区分专线和其他
                    home_path += zxidvalue + "/" + DateUtil.getToDayDate_1() + PicCopy.readProperties(tail);
                } else {
                    home_path += DateUtil.getToDayDate_1() + PicCopy.readProperties(tail);
                }
            } else {//每日，每周数据
                if (iszxjrFlag) {
                    home_path += zxidvalue + "/" + selectedDate + PicCopy.readProperties(tail);
                } else {
                    home_path += selectedDate + PicCopy.readProperties(tail);
                }
            }
            switch (tail) {
                case "se.360.jr.rt.tail":
                case "baidu.zhutie.rt.tail":
                case "se.baidu.jr.rt.tail":
                case "se.sougou.jr.rt.tail":
                case "forum.tianya.rt.tail":
                case "jr.meipian.rt.tail":
                case "jr.zhihu.rt.tail":
                case "jr.yaowen.rt.tail":
                    web_path = System.getProperty("user.dir") + "/" + PicCopy.readProperties(webpath) + "4/";
                    break;
                case "se.360.jr.daily.tail":
                case "baidu.zhutie.daily.tail":
                case "se.baidu.jr.daily.tail":
                case "se.sougou.jr.daily.tail":
                case "forum.tianya.daily.tail":
                case "jr.meipian.daily.tail":
                case "jr.zhihu.daily.tail":
                case "jr.yaowen.daily.tail":
                    web_path = System.getProperty("user.dir") + "/" + PicCopy.readProperties(webpath) + selectedDate + "/24/";
                    break;
                case "se.360.jr.weekly.tail":
                case "baidu.zhutie.weekly.tail":
                case "se.baidu.jr.weekly.tail":
                case "se.sougou.jr.weekly.tail":
                case "jr.meipian.weekly.tail":
                case "jr.zhihu.weekly.tail":
                case "jr.yaowen.weekly.tail":
                case "forum.tianya.weekly.tail":
                    web_path = System.getProperty("user.dir") + "/" + PicCopy.readProperties(webpath) + selectedDate + "/168/";
                    break;
                default:
                    logger.info("no this type of configuration");
                    out.print("0");
                    break;
            }
            logger.info("home_path = " + home_path);
            logger.info("web_path = " + web_path);//webapp.wx.network.path
            File file = new File(home_path);
            File[] files = file.listFiles();
            if (files == null || files.length == 0) {
                logger.info("this is no file in " + home_path);
                out.print("0");
            } else {

                String fileStrArray = "";
                for (int i = 0; i < files.length; i++) {
                    String filename = files[i].getName();
                    File fileDir = new File(web_path);
                    if (!fileDir.isDirectory()) {
                        fileDir.mkdirs();
                        logger.info(web_path);
                    }
                    File f = new File(web_path + filename);
                    if (f.exists()) {
                        PicCopy.copyFile(files[i], f);
                    } else {
                        try {
                            f.createNewFile();
                            PicCopy.copyFile(files[i], f);
                        } catch (IOException ex) {
                            logger.error(ex.getMessage(), ex);
                        }
                    }
                }
                String[] fs = file.list();
                Arrays.sort(fs);
                for (int i = 0; i < fs.length; i++) {
                    fileStrArray += fs[i] + ";";
                }
                out.print(fileStrArray);
            }
//            out.close();

        } catch (IOException ex) {
            logger.error(ex.getMessage(), ex);
        } finally {
            out.close();
        }
        return "success";
    }
//待定
//baidu_weekData
    public String msgData() throws IOException {

        HttpServletRequest request = ServletActionContext.getRequest();
        String data_type = request.getParameter("data_type");        //模拟数据
        String data_mpp = request.getParameter("data_mpp");
        logger.info("datatype:" + data_type);
        String msgData = request.getParameter("msgData");
        logger.info(msgData);
        String redisHosts = "";
        String dbName = "";
        String masterName = "";
        if (msgData.equals("yizhuang")) {
            redisHosts = PicCopy.readProperties("yz.redis.hosts");
            dbName = PicCopy.readProperties("yz.redis.db.name");
            masterName = PicCopy.readProperties("yz.redis.master.name");
        } else {
            redisHosts = PicCopy.readProperties("redis.hosts");
            dbName = PicCopy.readProperties("redis.db.name");
            masterName = PicCopy.readProperties("redis.master.name");
        }
        logger.info("redisHosts = "+redisHosts+";dbName = "+dbName+";masterName = "+masterName
        +",data_type = "+data_type+";data_mpp = "+data_mpp);
        RedisUtil userRedisUtil = new RedisUtil(redisHosts, masterName, Integer.parseInt(dbName));
        //接入
        String allCount_6 = userRedisUtil.get(data_type + DateUtil.getDate(-6));
        String allCount_5 = userRedisUtil.get(data_type + DateUtil.getDate(-5));
        String allCount_4 = userRedisUtil.get(data_type + DateUtil.getDate(-4));
        String allCount_3 = userRedisUtil.get(data_type + DateUtil.getDate(-3));
        String allCount_2 = userRedisUtil.get(data_type + DateUtil.getDate(-2));
        String allCount_1 = userRedisUtil.get(data_type + DateUtil.getDate(-1));
        String allCount_0 = userRedisUtil.get(data_type + DateUtil.getToDayDate_1());
        //mpp入库
        String mppCount_6 = userRedisUtil.get(data_mpp + DateUtil.getDate(-6));
        String mppCount_5 = userRedisUtil.get(data_mpp + DateUtil.getDate(-5));
        String mppCount_4 = userRedisUtil.get(data_mpp + DateUtil.getDate(-4));
        String mppCount_3 = userRedisUtil.get(data_mpp + DateUtil.getDate(-3));
        String mppCount_2 = userRedisUtil.get(data_mpp + DateUtil.getDate(-2));
        String mppCount_1 = userRedisUtil.get(data_mpp + DateUtil.getDate(-1));
        String mppCount_0 = userRedisUtil.get(data_mpp + DateUtil.getToDayDate_1());
        logger.info("today data " + allCount_0 + "mpp" + mppCount_0);
        int todayAllCountIncr = 0, todayMppCountIncr = 0;

        if (mppCount_1 == null || mppCount_1.equals("")) {
            mppCount_1 = "0";
        }
        if (mppCount_2 == null || mppCount_2.equals("")) {
            mppCount_2 = "0";
        }
        if (mppCount_3 == null || mppCount_3.equals("")) {
            mppCount_3 = "0";
        }
        if (mppCount_4 == null || mppCount_4.equals("")) {
            mppCount_4 = "0";
        }
        if (mppCount_5 == null || mppCount_5.equals("")) {
            mppCount_5 = "0";
        }
        if (mppCount_6 == null || mppCount_6.equals("")) {
            mppCount_6 = "0";
        }
        if (allCount_1 == null || allCount_1.equals("")) {
            allCount_1 = "0";
        }
        if (allCount_2 == null || allCount_2.equals("")) {
            allCount_2 = "0";
        }
        if (allCount_3 == null || allCount_3.equals("")) {
            allCount_3 = "0";
        }
        if (allCount_4 == null || allCount_4.equals("")) {
            allCount_4 = "0";
        }
        if (allCount_5 == null || allCount_5.equals("")) {
            allCount_5 = "0";
        }
        if (allCount_6 == null || allCount_6.equals("")) {
            allCount_6 = "0";
        }
        if (allCount_0 == null || allCount_0.equals("")) {
            allCount_0 = "0";
        }
        if (mppCount_0 == null || mppCount_0.equals("")) {
            mppCount_0 = "0";
        }
        todayAllCountIncr = (Integer.parseInt(allCount_0) - Integer.parseInt(allCount_1));
        todayMppCountIncr = (Integer.parseInt(mppCount_0) - Integer.parseInt(mppCount_1));

        Object ob = JSON.toJSON("{\"categories\": [\"" + DateUtil.getDate(-6) + "\", \"" + DateUtil.getDate(-5) + "\", \"" + DateUtil.getDate(-4) + "\", \"" + DateUtil.getDate(-3) + "\", \"" + DateUtil.getDate(-2) + "\", \"" + DateUtil.getDate(-1) + "\", \"" + DateUtil.getToDayDate_1()
                + "\"],\"data\": ["
                + allCount_6 + ", " + allCount_5 + ", " + allCount_4 + ", " + allCount_3
                + ", " + allCount_2 + ", " + allCount_1 + ", " + allCount_0
                + "],\"incr\":[\"" + (Integer.parseInt(allCount_5) - Integer.parseInt(allCount_6))
                + "\",\"" + (Integer.parseInt(allCount_4) - Integer.parseInt(allCount_5))
                + "\",\"" + (Integer.parseInt(allCount_3) - Integer.parseInt(allCount_4))
                + "\",\"" + (Integer.parseInt(allCount_2) - Integer.parseInt(allCount_3))
                + "\",\"" + (Integer.parseInt(allCount_1) - Integer.parseInt(allCount_2)) + "\",\"" + (Integer.parseInt(allCount_5) - Integer.parseInt(allCount_6))
                + "\",\"" + todayAllCountIncr + "\"],"
                + "\"mpp\":["
                + mppCount_6 + ", " + mppCount_5 + ", " + mppCount_4 + ", " + mppCount_3
                + ", " + mppCount_2 + ", " + mppCount_1 + ", " + mppCount_0
                + "],\"mppincr\":[\""
                + (Integer.parseInt(mppCount_5) - Integer.parseInt(mppCount_6))
                + "\",\"" + (Integer.parseInt(mppCount_4) - Integer.parseInt(mppCount_5))
                + "\",\"" + (Integer.parseInt(mppCount_3) - Integer.parseInt(mppCount_4))
                + "\",\"" + (Integer.parseInt(mppCount_2) - Integer.parseInt(mppCount_3))
                + "\",\"" + (Integer.parseInt(mppCount_1) - Integer.parseInt(mppCount_2)) + "\",\"" + (Integer.parseInt(allCount_5) - Integer.parseInt(allCount_6))
                + "\",\"" + todayMppCountIncr
                + "\"]"
                + "}");
        logger.info(ob);
        HttpServletResponse response = ServletActionContext.getResponse();
        response.reset();
        response.setContentType("text/html;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");//防止弹出的信息出现乱码getjobinfo
        PrintWriter out = response.getWriter();
        out.print(ob);
        out.close();
        return "success";
    }
//baidu_monthData
    public String monthData() throws IOException {
        HttpServletRequest request = ServletActionContext.getRequest();
        String data_type = request.getParameter("data_type");        //模拟数据
        String data_mpp = request.getParameter("data_mpp");

        String monthData = request.getParameter("monthData");
        logger.info(monthData);
        String redisHosts = "";
        String dbName = "";
        String masterName = "";
        if (monthData.equals("yizhuang")) {
            redisHosts = PicCopy.readProperties("yz.redis.hosts");
            dbName = PicCopy.readProperties("yz.redis.db.name");
            masterName = PicCopy.readProperties("yz.redis.master.name");
        } else {
            redisHosts = PicCopy.readProperties("redis.hosts");
            dbName = PicCopy.readProperties("redis.db.name");
            masterName = PicCopy.readProperties("redis.master.name");
        }
        logger.info("redisHosts = "+redisHosts+";dbName = "+dbName+";masterName = "+masterName
        +",data_type = "+data_type+";data_mpp = "+data_mpp);
        RedisUtil userRedisUtil = new RedisUtil(redisHosts, masterName, Integer.parseInt(dbName));
        HashMap<String, String> hashMap = new HashMap<>();
        String categories = "{\"categories\": [\"";
        String allCount = "\"data\": [";
        String incr = "\"incr\":[\"";
        String mppCount = "\"mpp\": [";
        String mppincr = "\"mppincr\":[\"";
//        java.util.Random r = new java.util.Random();
        for (int i = -30; i < 1; i++) {
//            String twx_user_allcount = String.valueOf(r.nextInt(100000));
//            String twx_user_allcount_1 = String.valueOf(r.nextInt(100000));
            hashMap.put(DateUtil.getDate(i), userRedisUtil.get(data_type + DateUtil.getDate(-i)));
            String twx_user_allcount = userRedisUtil.get(data_type + DateUtil.getDate(i));
            String twx_user_allcount_1 = userRedisUtil.get(data_type + DateUtil.getDate(i - 1));
            String baidu_mppcount = userRedisUtil.get(data_mpp + DateUtil.getDate(i));
            String baidu_mppcount_1 = userRedisUtil.get(data_mpp + DateUtil.getDate(i - 1));

            if (twx_user_allcount == null || twx_user_allcount.equals("")) {
                twx_user_allcount = "0";
            }
            if (twx_user_allcount_1 == null || twx_user_allcount_1.equals("")) {
                twx_user_allcount_1 = "0";
            }
            if (baidu_mppcount == null || baidu_mppcount.equals("")) {
                baidu_mppcount = "0";
            }
            if (baidu_mppcount_1 == null || baidu_mppcount_1.equals("")) {
                baidu_mppcount_1 = "0";
            }
            if (i < 0) {
                categories += DateUtil.getDate(i) + "\", \"";
                allCount += twx_user_allcount + ", ";
                incr += Integer.parseInt(twx_user_allcount) - Integer.parseInt(twx_user_allcount_1) + "\",\"";
                mppCount += baidu_mppcount + ",";
                mppincr += Integer.parseInt(baidu_mppcount) - Integer.parseInt(baidu_mppcount_1) + "\",\"";
            } else {
                categories += DateUtil.getDate(i) + "\"],";
                allCount += twx_user_allcount + "],";
                incr += Integer.parseInt(twx_user_allcount) - Integer.parseInt(twx_user_allcount_1) + "\"],";
                mppCount += baidu_mppcount + "],";
                mppincr += Integer.parseInt(baidu_mppcount) - Integer.parseInt(baidu_mppcount_1) + "\"]}";

            }
        }

        Object ob = JSON.toJSON(categories + allCount + incr + mppCount + mppincr);
        logger.info(ob.toString());
        HttpServletResponse response = ServletActionContext.getResponse();
        response.reset();
        response.setContentType("text/html;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");//防止弹出的信息出现乱码getjobinfo
        PrintWriter out = response.getWriter();
        out.print(ob);
        out.close();
        return "success";
    }

    public String getZxid() {
        return zxid;
    }

    public void setZxid(String zxid) {
        this.zxid = zxid;
    }

    public String getTrunk() {
        return trunk;
    }

    public void setTrunk(String trunk) {
        this.trunk = trunk;
    }

}
